import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { BasicService } from './basic.service';
import { HttpClient } from '@angular/common/http';
import { Song, SongSheet } from 'src/app/routes/entity';
import { map, pluck, switchMap } from 'rxjs/operators';
import { SongService } from './song.service';

@Injectable()
export class SongSheetService extends BasicService {

  constructor(
    private http: HttpClient,
    private songService: SongService
  ) {
    super();
  }

  /**
   * 获取歌曲集合
   * 
   * @param id 歌单 id
   */
  getSongSheets(id: number): Observable<Song[]> {
    return this.getSongSheetDetail(id)
      .pipe(
        pluck('tracks'),
        switchMap(tracks => this.songService.getSongList(tracks))
      );
  }

  /**
   * 获取歌单详情
   *
   * @param id 歌单 id
   */
  getSongSheetDetail(id: number): Observable<SongSheet> {
    const url = this.api(`playlist/detail?id=${id}`);
    return this.http.get(url)
      .pipe(map((res: { playlist: SongSheet }) => res.playlist));
  }
}
